package com.yiyahui.project.erp.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.yiyahui.project.erp.domain.YiyaClinicPerson;
import com.yiyahui.project.erp.domain.responseVo.DiagnoseRunningExtend;
import com.yiyahui.project.erp.service.IYiyaClinicPersonService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import com.yiyahui.framework.aspectj.lang.annotation.Log;
import com.yiyahui.framework.aspectj.lang.enums.BusinessType;
import com.yiyahui.project.erp.domain.YiyaDiagnoseRunning;
import com.yiyahui.project.erp.service.IYiyaDiagnoseRunningService;
import com.yiyahui.framework.web.controller.BaseController;
import com.yiyahui.framework.web.domain.AjaxResult;
import com.yiyahui.framework.web.domain.BaseEntity;
import com.yiyahui.common.utils.poi.ExcelUtil;
import com.yiyahui.framework.web.page.TableDataInfo;

/**
 * 看诊记录Controller
 * 
 * @author yiyahui
 * @date 2020-05-19
 */
@Api("看诊记录")
@RestController
@RequestMapping("/erp/running")
public class YiyaDiagnoseRunningController extends BaseController
{
    @Autowired
    private IYiyaDiagnoseRunningService yiyaDiagnoseRunningService;

    @Autowired
    private IYiyaClinicPersonService yiyaClinicPersonService;
 
    /**
     * 查询看诊记录列表
     */
    @ApiOperation("查询看诊记录列表")
    @PreAuthorize("@ss.hasPermi('erp:running:list')")
    @GetMapping("/list")
    public TableDataInfo list(YiyaDiagnoseRunning yiyaDiagnoseRunning)
    {
        startPage();
        List<YiyaDiagnoseRunning> list = yiyaDiagnoseRunningService.selectYiyaDiagnoseRunningList(yiyaDiagnoseRunning);
        return getDataTable(list);
    }

    /**
     * 导出看诊记录列表
     */
    @ApiOperation("导出看诊记录列表")
    @PreAuthorize("@ss.hasPermi('erp:running:export')")
    @Log(title = "看诊记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(YiyaDiagnoseRunning yiyaDiagnoseRunning)
    {
        List<YiyaDiagnoseRunning> list = yiyaDiagnoseRunningService.selectYiyaDiagnoseRunningList(yiyaDiagnoseRunning);
        ExcelUtil<YiyaDiagnoseRunning> util = new ExcelUtil<YiyaDiagnoseRunning>(YiyaDiagnoseRunning.class);
        return util.exportExcel(list, "running");
    }

    /**
     * 获取看诊记录详细信息
     */
    @ApiOperation("获取看诊记录详细信息")
    @ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "Long", paramType = "path")
    @PreAuthorize("@ss.hasPermi('erp:running:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yiyaDiagnoseRunningService.selectYiyaDiagnoseRunningById(id));
    }

    /**
     * 新增看诊记录
     */
    @ApiOperation("新增看诊记录")
    @ApiImplicitParam(name = "yiyaDiagnoseRunning", value = "${tableComment}", dataType = "YiyaDiagnoseRunning")
    @PreAuthorize("@ss.hasPermi('erp:running:add')")
    @Log(title = "看诊记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YiyaDiagnoseRunning yiyaDiagnoseRunning)
    {
        return toAjax(yiyaDiagnoseRunningService.insertYiyaDiagnoseRunning(yiyaDiagnoseRunning));
    }

    /**
     * 修改看诊记录
     */
    @ApiOperation("修改看诊记录")
    @ApiImplicitParam(name = "yiyaDiagnoseRunning", value = "${tableComment}", dataType = "YiyaDiagnoseRunning")
    @PreAuthorize("@ss.hasPermi('erp:running:edit')")
    @Log(title = "看诊记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YiyaDiagnoseRunning yiyaDiagnoseRunning)
    {
        return toAjax(yiyaDiagnoseRunningService.updateYiyaDiagnoseRunning(yiyaDiagnoseRunning));
    }

    /**
     * 删除看诊记录
     */
    @ApiOperation("删除看诊记录")
    @ApiImplicitParam(name = "ids", value = "主键id数组", required = true, dataType = "Long[]", paramType = "path")
    @PreAuthorize("@ss.hasPermi('erp:running:remove')")
    @Log(title = "看诊记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yiyaDiagnoseRunningService.deleteYiyaDiagnoseRunningByIds(ids));
    }


    /**
     * 自定义看诊记录查询
     */
    @ApiOperation("自定义看诊记录查询")
    @PostMapping("/extend/list")
    public TableDataInfo extendList(@RequestBody YiyaDiagnoseRunning yiyaDiagnoseRunning)
    {
        startPageByPost((BaseEntity)yiyaDiagnoseRunning);
        List<YiyaDiagnoseRunning> list = yiyaDiagnoseRunningService.selectYiyaDiagnoseRunningList(yiyaDiagnoseRunning);
        Map<Long, YiyaClinicPerson> yiyaClinicPersonMap = yiyaClinicPersonService.selectByIds(list.stream().map(YiyaDiagnoseRunning::getClinicPersonId).collect(Collectors.toList())).stream().collect(Collectors.toMap(YiyaClinicPerson::getId, a -> a, (k1, k2) -> k1));
        List<DiagnoseRunningExtend> diagnoseRunningExtends = new ArrayList<>();
        list.forEach(r -> {
            DiagnoseRunningExtend diagnoseRunningExtend = new DiagnoseRunningExtend();
            diagnoseRunningExtend.setYiyaDiagnoseRunning(r);
            diagnoseRunningExtend.setYiyaClinicPerson(yiyaClinicPersonMap.get(r.getClinicPersonId()));
            diagnoseRunningExtends.add(diagnoseRunningExtend);
        });
        return getDataTable(diagnoseRunningExtends,list);
    }
}
